package dao.impl;

import Service.ICategoryService;
import Service.IProductService;
import Service.impl.ProductServiceImpl;
import com.mysql.cj.PreparedQuery;
import dao.ICartEntryDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import pojo.CartEntry;
import pojo.ShopCart;
import pojo.User;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartEntryDaoImpl implements ICartEntryDao {
    @Override
    public void addCartEntry(CartEntry cartEntry) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            if (findByProductid(cartEntry) != null) {
                String sql1 = "update cartentry set count = ? where shopcartid = ? and productid=?";
                int count = findByProductid(cartEntry).getCount();
                queryRunner.update(connection, sql1, count + 1, cartEntry.getShopCart().getUser().getId(),cartEntry.getProduct().getProductid());
            } else {
                String sql = "insert into cartentry(productid,shopcartid,count) values (?,?,?)";
                queryRunner.update(connection, sql, cartEntry.getProduct().getProductid(), cartEntry.getShopCart().getUser().getId(), cartEntry.getCount());
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public CartEntry findByProductid(CartEntry cartEntry) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "select * from cartentry where shopcartid = ? and productid = ?";
            CartEntry cartEntry1 = queryRunner.query(connection, sql, new BeanHandler<>(CartEntry.class), cartEntry.getShopCart().getUser().getId(), cartEntry.getProduct().getProductid());
            connection.commit();
            return cartEntry1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CartEntry> findByShopCartId(User user) {
        QueryRunner queryRunner = new QueryRunner();
        Connection connection = null;
        List<CartEntry> cartEntryList =new ArrayList<CartEntry>();
        IProductService productService=new ProductServiceImpl();
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "select * from cartentry where shopcartid = ?";
            List<Object[]> objects= queryRunner.query(FirstApp.getConn(), sql, new ArrayListHandler(), user.getId());
            System.out.println(objects.size());
            for(int i=0;i<objects.size();i++)
            {
                CartEntry cartEntry=new CartEntry();
                cartEntry.setCartEntryid((Integer) objects.get(i)[0]);
                cartEntry.setProduct(productService.findById((Integer) objects.get(i)[1]));
                ShopCart shopCart=new ShopCart();
                shopCart.setShopcartid(user.getId());
                shopCart.setUser(user);
                cartEntry.setShopCart(shopCart);
                cartEntry.setCount((Integer) objects.get(i)[3]);
                cartEntryList.add(cartEntry);
            }
            connection.commit();
            return cartEntryList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
